//
//  KFDUpdatePasswordViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/1/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDUpdatePasswordViewController.h"
#import "UIButton+KFD.h"
#import "ui_config.h"
#import "KFDUIConfigurator.h"
#import "KFDAccountManager.h"
#import "JTToast.h"

@interface KFDUpdatePasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordAgainTextField;
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (strong,nonatomic) UIButton* completeSettingButton;

@end

@implementation KFDUpdatePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"设置新密码";
    [self setupCompleteSettingButton];
}

-(void)setupCompleteSettingButton
{
    self.completeSettingButton=[UIButton redButtonWithtitle:@"完成设置"];
    self.completeSettingButton.center=CGPointMake(SCREEN_WIDTH/2, 60);
    [self.completeSettingButton addTarget:self action:@selector(completeSettingButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.completeSettingButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)goBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)completeSettingButtonPressed
{
    [self.passwordTextField resignFirstResponder];
    [self.passwordAgainTextField resignFirstResponder];
    NSString* password=self.passwordTextField.text;
    NSString* passwordAgain=self.passwordAgainTextField.text;
    KFDAccountManager* manager=[KFDAccountManager sharedManager];
    if ([manager checkPassword:password illegalInputHandler:^(NSString *message) {
        [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
    }]) {
        if ([password isEqualToString:passwordAgain]&&self.token) {
            [manager updatePassword:password token:self.token successHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self goBack];
                });
            } failureHandler:^(KFDResponseBody *response){
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
                });
            }];
        }
    }
}

@end
